package ai

import (
	"encoding/json"
	"fmt"

	http_label_center "gobase/http_work_flow/label_center"
	facex "gobase/http_work_flow/label_center/ai/face"
)

// 数据转绘制
func Data2FaceMidentify(value *facex.RespFaceMidentify) (_frame http_label_center.Tag, _err error) {
	var metas = make([]http_label_center.Meta, 0)
	if value.ErrorCode == 222207 {
		var meta = http_label_center.Meta{
			X:           1920 / 4,
			Y:           1080 / 4,
			Remark:      "多人脸搜索",
			TypeId:      http_label_center.IdScheme多人脸搜索,
			ProtoWidth:  1920,
			ProtoHeight: 1080,
			Rect: &http_label_center.Rect{
				Width:  1920 / 2,
				Height: 1080 / 2,
				Text:   "未注册人员闯入",
				Center: false,
			},
		}
		meta.Rect.Text = "未注册人员闯入"
		metas = append(metas, meta)
	} else {
		if len(value.Result.FaceList) == 0 {
			_err = fmt.Errorf("没有搜索到到人脸 %+v", value.Result)
			return
		}
		for _, v := range value.Result.FaceList {
			var meta = http_label_center.Meta{
				X:      int64(v.Location.Left),
				Y:      int64(v.Location.Top),
				Remark: "多人脸搜索",
				TypeId: http_label_center.IdScheme多人脸搜索,
				Rect: &http_label_center.Rect{
					Width:  int64(v.Location.Width),
					Height: int64(v.Location.Height),
					Text:   v.UserInfoEx,
					Center: false,
				},
			}
			var userInfo facex.UserInfo
			err := json.Unmarshal([]byte(v.UserInfoEx), &userInfo)
			if err != nil {
				continue
			}
			meta.Rect.Text = userInfo.UserName
			metas = append(metas, meta)
		}
	}
	_frame = http_label_center.Tag{
		Metas: metas,
		AbsoluteTime: &http_label_center.AbsoluteTime{
			CenterUnixMs: int64(value.Timestamp * 1000),
			DurationMs:   2000,
		},
	}
	return
}

// 数据转绘制
func Data2FaceLiveness(value *facex.RespFaceLiveness) (_frame http_label_center.Tag, _err error) {
	var metas = make([]http_label_center.Meta, 0)
	if len(value.Result.FaceList) == 0 {
		_err = fmt.Errorf("没有搜索到到人脸 %+v", value.Result)
		return
	}
	for _, v := range value.Result.FaceList {
		var meta = http_label_center.Meta{
			X:      int64(v.Location.Left),
			Y:      int64(v.Location.Top),
			Remark: "活体检测",
			TypeId: http_label_center.IdScheme活体检测,
			// ProtoWidth:  1920,
			// ProtoHeight: 1080,
			Rect: &http_label_center.Rect{
				Width:  int64(v.Location.Width),
				Height: int64(v.Location.Height),
				Text:   v.UserInfoEx,
				Center: false,
			},
		}
		var userInfo facex.UserInfo
		err := json.Unmarshal([]byte(v.UserInfoEx), &userInfo)
		if err != nil {
			continue
		}
		meta.Rect.Text = userInfo.UserName
		metas = append(metas, meta)
	}

	_frame = http_label_center.Tag{
		Metas: metas,
		AbsoluteTime: &http_label_center.AbsoluteTime{
			CenterUnixMs: int64(value.Timestamp * 1000),
			DurationMs:   2000,
		},
	}
	return
}
